package com.quanyan.common.validation.validator;

import com.quanyan.common.validation.enums.Money;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

/**
 * Created by Zheng.Ke
 * Date 2016/8/29.
 */
public class MoneyFormatValidator implements ConstraintValidator<Money, BigDecimal> {

    @Override
    public void initialize(Money constraintAnnotation) {

    }

    @Override
    public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }
        // 小数点后从第三数开始，不能有非0值
        if (value.doubleValue() == (Math.floor(value.multiply(new BigDecimal("100")).doubleValue())/100)) {
            return true;
        }
        return false;
    }
}
